package com.edu.leetcode.dataStruct;


/**
 * @作者 five-five
 * @创建时间 2021/6/8
 */
public class CodePointConversion2 {
    private String regix = "^([-+]?(([0-9]+)([.]([0-9]+))?|([.]([0-9]+))?))$";

    public static void main(String[] args) {
        CodePointConversion2 codePointConversion = new CodePointConversion2();
        String x = codePointConversion.convertText(" (2502) A 2502 2494 2502");
        System.out.println(x);
        System.out.println(String.valueOf(Character.toChars(2502)));
        System.out.println(String.valueOf(Character.toChars(2494)));
        System.out.println(String.valueOf(Character.toChars(2502)));
    }

    /**
     * @param str e.g. (2502) A 2502 A 2502
     * @return 转换后的简体
     */
    public String convertText(String str) {
        StringBuilder stringBuilder = new StringBuilder();
        if (str.substring(0, 1).equals(" ")) {
            str = str.substring(1);
        }
        String[] s = str.split(" ");
        int length = s.length;
        for (int i = 0; i < length; i++) {
            //同时为true
            String s1 = s[i];
            if (isOurWant(s1) && (i+1<length?isOurWant(s[i + 1]):false)) {
                char c = s1.charAt(0);
                char c1 = s1.charAt(s1.length() - 1);
                if (c == '(') {
                    s1 = s1.substring(1);
                    stringBuilder.append("(").append("===");
                }
                boolean flag=false;
                if (c1 == ')') {
                    flag = true;
                    s1 = s1.substring(0, s1.length() - 1);
                    stringBuilder.append(")").append("===");
                }
                stringBuilder.insert(flag?stringBuilder.length() - 1:0, String.valueOf(Character.toChars(Integer.parseInt(s1)))).append("===");
            }
            //最后一位数字
            if ((i == length - 1) && isOurWant(s1)) {
                stringBuilder.append(String.valueOf(Character.toChars(Integer.parseInt(s1)))).append("===");
            }
        }
        return stringBuilder.toString();
    }

    /**
     * 判断该字符是否是想要的
     *
     * @param str
     * @return
     */
    private boolean isOurWant(String str) {
        if (str.length() >= 4) {
            if (str.matches(regix)) {
                return true;
            }
            char c = str.charAt(0);
            char c1 = str.charAt(str.length() - 1);
            if (c == '(') {
                str = str.substring(1);
            }
            if (c1 == ')') {
                str = str.substring(0, str.length() - 1);
            }
            return str.matches(regix);
        }
        return false;
    }

}
